package com.supcon.exchangedata.pojo.entity;

import com.alibaba.fastjson.annotation.JSONField;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @program: zhyy
 * @description:
 * @author: cherubr.Sumin
 * @create: 2021-08-10 10:32
 **/
public class AjaxMessage<T> implements Serializable {

    private static final long serialVersionUID = -7295464290183189928L;
    public static final String STATUS_SUCCESS = "success";
    public static final String STATUS_FAIL = "fail";
    public static final String STATUS_ERROR = "error";
    public static final String STATUS_WARN = "warn";
    private String status;
    private T data;
    private String message;

    public AjaxMessage(String status) {
        this.status = status;
    }

    @JSONField(
            serialize = false
    )
    public boolean isSuccess() {
        return "success".equals(this.getStatus());
    }

    public static <T> AjaxMessage success(T data) {
        AjaxMessage am = new AjaxMessage("success");
        am.setData(data);
        return am;
    }

    public static AjaxMessage success() {
        AjaxMessage am = new AjaxMessage("success");
        return am;
    }

    public static AjaxMessage failue(Map<String, String> errors) {
        AjaxMessage am = new AjaxMessage("fail");
        Map<String, Map<String, String>> data = new HashMap(1);
        data.put("errors", errors);
        am.setData(data);
        return am;
    }

    public static AjaxMessage error(String message) {
        AjaxMessage am = new AjaxMessage("error");
        am.setMessage(message);
        return am;
    }

    public static AjaxMessage warn(String message) {
        AjaxMessage am = new AjaxMessage("warn");
        am.setMessage(message);
        return am;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}
